package com.ysstech.reportworld.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.reportworld.entity.Table;
import com.ysstech.reportworld.entity.TableTable;
import com.ysstech.reportworld.service.TableTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 集市层表依赖表信息 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2022-05-25
 */
@Controller
@Slf4j
@Api(tags = "报表世界表依赖管理api")
@RequestMapping("/tableTable")
public class TableTableController extends BaseController {

    @Autowired
    private TableTableService tableTableService;


    /**
     * 查询集市层依赖表集合信息
     *
     * @return
     */
    @ApiOperation(value = "获取集市层依赖表集合信息")
    @YssLog(module = "报表管理", remark = "获取集市层依赖表集合信息")
    @RequestMapping(value = "/queryTableTable")
    @ResponseBody
    public RestResult queryTableTable(Table table) throws Exception {
        if (null == table || StringUtils.isBlank(table.getId())) {
            throw new BusinessException("没有获取到对应依赖表信息，请稍后重试！");
        }
        return ResultGenerator.getSuccessResult("获取集市层依赖表集合信息成功！", tableTableService.queryTableTable(table));
    }


    @ApiOperation(value = "获取集市层依赖表集合信息")
    @YssLog(module = "报表管理", remark = "获取集市层依赖表集合信息")
    @RequestMapping(value = "/queryTableTableByDetail")
    @ResponseBody
    public RestResult queryTableTableByDetail(Table table) throws Exception {
        if (null == table || StringUtils.isBlank(table.getId())) {
            throw new BusinessException("没有获取到对应依赖表信息，请稍后重试！");
        }
        List<Table> list = tableTableService.queryTableTable(table);
        if (!CollectionUtils.isEmpty(list)) {
            for (Table table1 : list) {
                if (null == table1) {
                    continue;
                }
                table1.setChineseName(table1.getName() + "-" + table1.getChineseName());
            }
        }
        return ResultGenerator.getSuccessResult("获取集市层依赖表集合信息成功！", list);
    }

    /**
     * 查询字段依赖表集字段信息
     *
     * @return
     */
    @ApiOperation(value = "获取集市层依赖表集合信息")
    @YssLog(module = "报表管理", remark = "获取集市层依赖表集合信息")
    @RequestMapping(value = "/queryTableTableDetail")
    @ResponseBody
    public RestResult queryTableTableDetail(Table table) throws Exception {
        if (null == table || StringUtils.isBlank(table.getColumnId())) {
            throw new BusinessException("没有获取到对应依赖表信息，请稍后重试！");
        }
        return ResultGenerator.getSuccessResult("获取集市层依赖表集合信息成功！", tableTableService.queryTableTableDetail(table));
    }

    /**
     * 新增表依赖的方法
     *
     * @param tableTable
     * @return
     */
    @ApiOperation(value = "新增表依赖信息")
    @YssLog(module = "表依赖管理", remark = "新增表依赖信息")
    @RequestMapping(value = "/add")
    @ResponseBody
    public RestResult add(TableTable tableTable) throws Exception {
        if (null == tableTable) {
            throw new BusinessException("获取新增报表世界表依赖信息失败，请稍后重试！");
        }
        tableTable.setId(DateUtil.generateUUID());
        tableTable.setAddUser(redisCacheManager.getLoginUserInfo().getLoginId());
        tableTable.setAddTime(LocalDateTime.now());
        tableTableService.save(tableTable);
        return ResultGenerator.getSuccessResult("新增表依赖信息成功！");
    }

    /**
     * 修改表依赖信息
     *
     * @return
     */
    @ApiOperation(value = "修改表依赖信息")
    @YssLog(module = "表依赖管理", remark = "修改表依赖信息")
    @RequestMapping(value = "/edit")
    @ResponseBody
    public RestResult edit(TableTable tableTable) throws Exception {
        if (tableTable == null || StringUtils.isBlank(tableTable.getId())) {
            throw new BusinessException("获取表依赖信息失败，请稍后重试！");
        }
        tableTable.setEditUser(redisCacheManager.getLoginUserInfo().getLoginId());
        tableTable.setEditTime(LocalDateTime.now());
        tableTableService.updateById(tableTable);
        tableTable.setEditTime(LocalDateTime.now());
        return ResultGenerator.getSuccessResult("修改表依赖信息成功！");
    }

    /**
     * 删除表依赖信息
     *
     * @return
     */
    @ApiOperation(value = "删除表依赖信息")
    @YssLog(module = "表依赖管理", remark = "删除表依赖信息")
    @RequestMapping(value = "/del")
    @ResponseBody
    public RestResult del(String ids) throws Exception {
        if (StringUtils.isBlank(ids)) {
            throw new BusinessException("获取删除定时任务信息失败，请稍后重试！");
        }
        List<String> listId = JSONArray.parseArray(ids, String.class);
        // 删除的是关联的表， 字段的关联也要删除
        QueryWrapper<TableTable> wrapper = new QueryWrapper<>();
        if (CollectionUtils.isEmpty(listId)) {
            return ResultGenerator.getSuccessResult("删除表依赖信息成功！");
        }
        for (String id : listId) {
            if (StringUtils.isBlank(id)) {
                continue;
            }
            // 查询数据的被依赖ID 和 表ID 删除对应数据
            wrapper.eq("ID", id);
            TableTable tableTable = tableTableService.getOne(wrapper);
            // 明细数据删除的时候 不同步删除
            if (null == tableTable || StringUtils.isBlank(tableTable.getTableId())
                    || StringUtils.isBlank(tableTable.getTableIdRely()) || 2 == tableTable.getFtype()) {
                continue;
            }
            QueryWrapper<TableTable> wrapperdel = new QueryWrapper<>();
            wrapperdel.eq("TABLE_ID", tableTable.getTableId());
            wrapperdel.eq("TABLE_ID_RELY", tableTable.getTableIdRely());
            tableTableService.remove(wrapperdel);
        }
        tableTableService.removeByIds(listId);
        return ResultGenerator.getSuccessResult("删除表依赖信息成功！");
    }
}
